package sysytmRepo

import (
	"gorm.io/gorm"
	"ruoyi/domain/entity/common"
	"ruoyi/domain/entity/response"
	"ruoyi/domain/entity/system"
	"ruoyi/domain/repository"
)

type SysDeptRepo struct {
	db *gorm.DB
}

func SysDeptRepository(db *gorm.DB) *SysDeptRepo {
	return &SysDeptRepo{db}
}

var _ repository.SysDeptRepository = &SysDeptRepo{}

// 分页查询
func (r *SysDeptRepo) GetDeptList(params common.SearchTableDataParam, isPaging bool) ([]system.SysDeptResult, int64) {
	var total int64
	var pageNum = params.PageNum
	var pageSize = params.PageSize
	offset := (pageNum - 1) * pageSize
	dept := params.Other.(system.SysDept)

	var list []system.SysDeptResult
	var db = r.db.Model(&[]system.SysDept{})

	var deptId = dept.DeptId
	if deptId != 0 {
		db.Where("dept_id = ?", deptId)
	}

	var parentId = dept.ParentId
	if parentId != 0 {
		db.Where("parent_id = ?", parentId)
	}

	var deptName = dept.DeptName
	if deptName != "" {
		db.Where("dept_name like concat('%', ?, '%')", deptName)
	}

	var status = dept.Status
	if status != "" {
		db.Where("status = ?", status)
	}
	db.Order("parent_id, order_num")

	if err := db.Count(&total).Error; err != nil {
		return nil, 0
	}
	if isPaging {
		if err := db.Offset(offset).Limit(pageSize).Find(&list).Error; err != nil {
			return nil, 0
		}
	} else {
		if err := db.Find(&list).Error; err != nil {
			return nil, 0
		}
	}

	return list, total
}

func (r *SysDeptRepo) GetDeptInfo(deptId string) system.SysDept {
	var dept system.SysDept
	err := r.db.Where("dept_id = ?", deptId).First(&dept).Error
	if err != nil {
		panic(response.ReturnFailMsg(err.Error()))
	}
	return dept
}

func (r *SysDeptRepo) SaveDept(sysDept system.SysDept) response.Response {
	sysDept.DelFlag = "0"
	err := r.db.Model(&sysDept).Create(&sysDept).Error
	if err != nil {
		return response.ReturnFailMsg(err.Error())
	}
	return response.ReturnSuccess("操作成功")
}

func (r *SysDeptRepo) UpDataDept(sysDept system.SysDept) response.Response {
	err := r.db.Updates(&sysDept).Error
	if err != nil {
		return response.ReturnFailMsg(err.Error())
	}
	return response.ReturnSuccess("操作成功")
}

/*单个删除*/
func (r *SysDeptRepo) DeleteDept(deptId string) response.Response {
	//if (deptService.hasChildByDeptId(deptId))
	//        {
	//            return AjaxResult.error("存在下级部门,不允许删除");
	//        }
	//        if (deptService.checkDeptExistUser(deptId))
	//        {
	//            return AjaxResult.error("部门存在用户,不允许删除");
	//        }
	//是否有数据权限
	err := r.db.Where("dept_id = ?", deptId).Delete(&system.SysDept{}).Error
	if err != nil {
		return response.ReturnFailMsg(err.Error())
	}
	return response.ReturnSuccess("操作成功")
}

func (r *SysDeptRepo) SelectDeptTreeList() []system.TreeSelect {
	var deptResults []system.TreeSelect
	var depts []system.SysDept
	err := r.db.Where("del_flag = '0'").Order("parent_id, order_num").Find(&depts).Error
	if err != nil {
		panic(response.ReturnFailMsg(err.Error()))
	}

	for i := 0; i < len(depts); i++ {
		dept := depts[i]
		deptId := dept.DeptId
		name := dept.DeptName
		pId := dept.ParentId
		if pId == 0 {
			tChild := r.GetChildList(depts, deptId)
			treeSelect := system.TreeSelect{
				Id:       deptId,
				Label:    name,
				Children: tChild,
			}
			deptResults = append(deptResults, treeSelect)
		}
	}

	return deptResults
}

func (r *SysDeptRepo) GetChildList(deptS []system.SysDept, deptId int) []system.TreeSelect {
	var tList []system.TreeSelect
	for i := 0; i < len(deptS); i++ {
		dept1 := deptS[i]
		id := dept1.DeptId
		pId := dept1.ParentId
		name := dept1.DeptName

		if pId == deptId {
			tChild := r.GetChildList(deptS, id)
			tree := system.TreeSelect{
				Id:       id,
				Label:    name,
				Children: tChild,
			}
			tList = append(tList, tree)
		}

	}
	return tList
}
